package codeine.servlets;

import java.io.PrintWriter;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import codeine.configuration.directory.PeerStatusJson;
import codeine.configuration.directory.PeersListJson;
import codeine.peers_status.PeersListStore;
import codeine.servlet.AbstractServlet;

public class PeerListServlet extends AbstractServlet {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(PeerListServlet.class);

	@Inject	private PeersListJson peersList;
	@Inject	private PeersListStore PeersListStore;

	@Override
	protected void myPost(HttpServletRequest req, HttpServletResponse resp){
		PeerStatusJson peerStatus = readBodyJson(req, PeerStatusJson.class);
		peerStatus.update_time(System.currentTimeMillis());
		log.info("recieved peer status " + peerStatus);
		peersList.update(peerStatus);
		PeersListStore.store();
	}
	
	@Override
	protected void myGet(HttpServletRequest req, HttpServletResponse resp){
		PrintWriter writer = getWriter(resp);
		writer.println(gson().toJson(peersList));
	}
}
